﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBLClient.DBLSearchService;
namespace DBLClient.DBLForm
{
    public partial class EditGuessWordForm : Form
    {
        public EditGuessWordForm()
        {
            InitializeComponent();
        }

        private void buttonSearchHotword_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBoxSearchHotword.Text.Trim()))
            {
                SearchService service = new SearchService();
                DataSet ds = service.GetHotWord(textBoxSearchHotword.Text.Trim());
                dataGridViewHotword.DataSource = ds.Tables[0];
            }
            else
            {
                MessageBox.Show("请输入搜索的热门词");
            }
        }

        private void buttonSearchGuessWord_Click(object sender, EventArgs e)
        {
            listBoxGuessWord.Items.Clear();
            SearchService service = new SearchService();
            if (!string.IsNullOrEmpty(textBoxSearchGuessWord.Text.Trim()))
            {

                DataSet ds = service.GetGuessWordFromDB(textBoxSearchGuessWord.Text.Trim());
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        listBoxGuessWord.Items.Add(dr["guessword"].ToString());
                    }
                }
            }
            else
            {
                MessageBox.Show("请输入搜索词");
            }
            
        }

        private void buttonAddEdit_Click(object sender, EventArgs e)
        {
            //把hotword放入编辑区
            if (dataGridViewHotword.CurrentRow == null)
            {
                MessageBox.Show("请选择放入放入编辑区的热门词");
                return;
            }
            textBoxEditGuessWord.Text = dataGridViewHotword.CurrentRow.Cells["ColumnHotword"].Value.ToString();

        }

        private void buttonRemoveGuessWord_Click(object sender, EventArgs e)
        {
            //删除猜测词，从数据库中删除
            if (listBoxGuessWord.SelectedIndex != -1)
            {
                SearchService service = new SearchService();
                if(service.DeleteGuessWordByWord(listBoxGuessWord.SelectedItem.ToString()) == 1)
                {
                    MessageBox.Show("删除成功");
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            
        }

        private void buttonAddGuessWord_Click(object sender, EventArgs e)
        {
            //新增猜测词，写入数据库
            if (textBoxEditGuessWord.Text != "")
            {
                SearchService service = new SearchService();
                if (service.AddGuessWord(textBoxEditGuessWord.Text) == 1)
                {
                    MessageBox.Show("新增成功");
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            else
            {
                MessageBox.Show("请输入猜测词");
            }
        }
    }
}
